package com.sgb.kjwl.view.ui.activity.self.publish;

import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityMachinePublishAttributeBinding;
import com.sgb.jicai.entity.FilterEntity;
import com.sgb.kjwl.model.entity.user.PublicAttrEntity;
import com.sgb.kjwl.view.event.PickEvent;
import com.sgb.kjwl.view.ui.adapter.self.publish.MachinePublishAttributeAdapter;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.swgk.core.event.EventConstant.EVENT_PUBLISH_DEVICE_ATTR;
import static com.swgk.core.event.EventConstant.EVENT_PUBLISH_MACHINE_ATTR;

public class PublicPublishAttributeActivity extends AppActivity {
    private ActivityMachinePublishAttributeBinding binding;
    private MachinePublishAttributeAdapter machinePublishAttributeAdapter;
    private List<String> stringList;
    private List<FilterEntity<List<FilterEntity>>> list;
    private List<FilterEntity> attrChildList;
    private List<PublicAttrEntity> attributesEntityList;
    private Map map;
    private int type;
    private int selectPosition;
    public static void start(Context context) {
        start(context, null);
    }

    public static void start(Context context, Intent extras) {
        Intent intent = new Intent();
        intent.setClass(context, PublicPublishAttributeActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        if (extras != null) {
            intent.putExtras(extras);
        }
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(PublicPublishAttributeActivity.this, R.layout.activity_machine_publish_attribute);
        binding.backView.setOnClickListener(this);
        type = getIntent().getIntExtra("type", 0);
        list = (List<FilterEntity<List<FilterEntity>>>) getIntent().getSerializableExtra("list");
        attributesEntityList = (List<PublicAttrEntity>) getIntent().getSerializableExtra("attributesEntityList");
    }

    @Override
    protected void init() {
        stringList = new ArrayList<>();
        map = new HashMap();
        machinePublishAttributeAdapter = new MachinePublishAttributeAdapter(PublicPublishAttributeActivity.this, list, attributesEntityList);
        binding.recycler.setLayoutManager(new LinearLayoutManager(PublicPublishAttributeActivity.this));
        binding.recycler.setAdapter(machinePublishAttributeAdapter);
        machinePublishAttributeAdapter.setOnItemClickListener((position, val) -> {
            setSoftWareHide();
            if (list.get(position).getAttrKey() == 1) {
                attrChildList = list.get(position).getChildren();
                for (int i = 0; i < attrChildList.size(); i++) {
                    if (attrChildList.get(i).getStartVal().equals(val)) {
                        selectPosition = i;
                        break;
                    }
                }
                showArrtPop(position);
            }
        });
        machinePublishAttributeAdapter.setFilterResultClick(datas -> {
            attributesEntityList = datas;
            if (type == EVENT_PUBLISH_MACHINE_ATTR) {
                EventBus.getDefault().post(PickEvent.make().setMessageTag(EVENT_PUBLISH_MACHINE_ATTR).setObject(attributesEntityList));
            } else {
                EventBus.getDefault().post(PickEvent.make().setMessageTag(EVENT_PUBLISH_DEVICE_ATTR).setObject(attributesEntityList));
            }
            finish();
        });
    }

    //机械属性型号选择
    private void showArrtPop(int item) {
        OptionsPickerView businessOption = new OptionsPickerBuilder(PublicPublishAttributeActivity.this, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                if (attrChildList.size() != 0) {
                    String content = attrChildList.get(options1).getStartVal();
                    machinePublishAttributeAdapter.setVal(content, item);
                    map.put(item, options1);
                }
            }
        }).build();
        businessOption.setPicker(attrChildList);
        if (map.get(item) == null) {
            businessOption.setSelectOptions(selectPosition);
        } else {
            businessOption.setSelectOptions((Integer) map.get(item));
        }
        businessOption.show();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_view:
                finish();
                break;
        }
    }
    /**
     * 隐藏软键盘
     */
    private void setSoftWareHide() {
        InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.hideSoftInputFromWindow(this.getWindow().getDecorView().getWindowToken(), 0);
        }
    }
}

